<template>
  <div>
    <div class="crumbs">
      <router-link to="/integral/goodsadd">
        <el-button>添加商品</el-button>
      </router-link>
    </div>
    <div class="crumbs">
      <el-form ref="form" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="商品标题">
          <el-input v-model="formInline.keywords" placeholder="请输入……" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch('form')">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100"></el-table-column>
      <el-table-column prop="title" label="商品标题" width="500"></el-table-column>
      <el-table-column prop="create_time" label="创建时间"></el-table-column>
      <el-table-column prop="status" label="状态" :formatter="statusFormat" width="120"></el-table-column>
      <el-table-column label="操作" width="330">
        <template slot-scope="scope">
          <el-button size="small" @click="edit(scope.$index, scope.row)">商品编辑</el-button>
          <el-button size="small" @click="failed(scope.$index, scope.row)">商品下架</el-button>
          <el-button size="small" @click="del(scope.$index, scope.row)">删除商品</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @current-change ="handleCurrentChange"
        layout="total, prev, pager, next"
        :current-page="textData.current_page"
        :page-size="textData.per_page"
        :total="textData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data () {
      return {
        formInline: {
          keywords: ''
        }
      }
    },
    computed: {
      ...mapState({
        tableData: state => state.mallgoods.list,
        textData: state => state.mallgoods.pageData
      })
    },
    created () {
      this.allGoodsList()
    },
    methods: {
      async allGoodsList () {
        let payload = {
          page: this.textData.current_page,
          listRows: this.textData.per_page,
          keywords: this.$route.query.keywords
        }
        await this.$store.dispatch('mallgoods/allGoodsList', payload)
      },
      onSearch (formName) {
        const self = this
        self.$refs[formName].validate((valid) => {
          if (valid) {
            self.$router.push({path: '/integral/goods', query: {keywords: self.formInline.keywords}})
            self.allGoodsList()
          }
        })
      },
      handleCurrentChange (val) {
        this.textData.current_page = val
        this.allGoodsList()
      },
      edit (index, row) {
        this.$store.commit('mallgoods/INTEGRAL_EDIT_SAVE_DATA', row)
        this.$router.push('/integral/goodsedit')
      },
      failed (index, row) {
        let payload = {
          id: row.id
        }
        this.$store.dispatch('mallgoods/soldOutOneGoods', payload).then(res => {
          this.allGoodsList()
          notify.success(res.msg)
        })
      },
      del (index, row) {
        let payload = {
          id: row.id
        }
        this.$store.dispatch('mallgoods/manageDelGoods', payload).then(res => {
          this.allGoodsList()
          notify.success(res.msg)
        })
      },
      statusFormat (value) {
        if (value.status === 1) {
          return '已通过'
        } else if (value.status === 3) {
          return '已下架'
        }
      }
    }
  }
</script>

<style scoped>
  .crumbs {
    display: inline-block;
    vertical-align: top;
    margin-right: 50px;
  }
</style>
